package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.InspectorDao;
import com.example.highway.dao.OwnerCarDao;
import com.example.highway.entity.Car;
import com.example.highway.entity.Inspector;
import com.example.highway.entity.OwnerCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("owner-car")
public class OwnerCarController {
    @Autowired
    OwnerCarDao ownerCarDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg){
//        JSONObject jsonObject = JSON.parseObject(msg);
//
//        Optional<OwnerCar> o = ownerCarDao.findById((String)jsonObject.get("id"));
//        if(o.isPresent()){
//            return new MyJson("0", false);
//        }
//
//        OwnerCar c = new OwnerCar();
//        c.setId((String)jsonObject.get("id"));
//        c.setcId((String)jsonObject.get("cId"));
//        c.setdINum((String)jsonObject.get("dINum"));
//        c.setIdNum((String)jsonObject.get("idNum"));
//        ownerCarDao.save(c);
//
//        return new MyJson("0", true);
        JSONObject jsonObject = JSON.parseObject(msg);

        OwnerCar car = ownerCarDao.save(jsonObject.toJavaObject(OwnerCar.class));
        JSONObject rtn = new JSONObject();
        rtn.put("id", car.getId());
        return new MyJson("0", rtn);
    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        if(jsonObject.containsKey("id")){
//            Optional<OwnerCar> o = ownerCarDao.findById((String)jsonObject.get("id"));
//            if(!o.isPresent()){
//                return new MyJson("0", null);
//            }else {
//                OwnerCar ownerCar = o.get();
//                return new MyJson("0", ownerCar);
//            }
//        }
//        OwnerCar ownerCar = jsonObject.toJavaObject(OwnerCar.class);
//
//        List<OwnerCar> ownerCarList = ownerCarDao.findAll(Example.of(ownerCar));
        OwnerCar ownerCar = jsonObject.toJavaObject(OwnerCar.class);
        List<OwnerCar> ownerCars = ownerCarDao.findAll(Example.of(ownerCar));

        return new MyJson("0", JSONArray.toJSON(ownerCars));

    }

//    @RequestMapping("get-all")
//    public MyJson all(){
//        List<OwnerCar> o = ownerCarDao.findAll();
//        if(o.isEmpty()){
//            return new MyJson("0", null);
//        }
//
//        return new MyJson("0", o);
//
//    }
}
